{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "title: '`@staticmethod`'\n",
    "description: Declares a struct method as static.\n",
    "css: /static/styles/page-navigation.css\n",
    "website:\n",
    "  open-graph:\n",
    "    image: /static/images/mojo-social-card.png\n",
    "  twitter-card:\n",
    "    image: /static/images/mojo-social-card.png\n",
    "---"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can add the `@staticmethod` decorator on a struct method to declare a static\n",
    "method. \n",
    "\n",
    "For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensor import Tensor\n",
    "from pathlib import Path\n",
    "\n",
    "\n",
    "struct MyStruct:\n",
    "    var data: Tensor[DType.int8]\n",
    "\n",
    "    fn __init__(inout self):\n",
    "        self.data = Tensor[DType.int8]()\n",
    "\n",
    "    fn __moveinit__(inout self, owned existing: Self):\n",
    "        self.data = existing.data ^\n",
    "\n",
    "    @staticmethod\n",
    "    fn load_from_file(file_path: Path) raises -> Self:\n",
    "        var new_struct = MyStruct()\n",
    "        new_struct.data = file_path.read_bytes()\n",
    "        return new_struct ^"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Unlike an instance method, a static method doesn't take an implicit `self`\n",
    "argument. It's not attached to a specific instance of a struct, so it can't\n",
    "access instance data.\n",
    "\n",
    "For more information see the documentation on\n",
    "[static methods](/mojo/manual/structs.html#static-methods)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Mojo",
   "language": "mojo",
   "name": "mojo-jupyter-kernel"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "mojo"
   },
   "file_extension": ".mojo",
   "mimetype": "text/x-mojo",
   "name": "mojo"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
